package com.hy.ai.controller;

import com.hy.ai.repository.ChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;

@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class IkunController {
    private final ChatClient serviceChatClient ;
    private final ChatHistoryRepository chatHistoryRepository;
    @RequestMapping(value = "/service",produces = "text/html;charset=utf-8")
    public Flux<String> service(String prompt, String chatId){
        //保存会话Id
        chatHistoryRepository.save("service",chatId,"1");
        return serviceChatClient.prompt()
                .user(prompt)
                .advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)) //设置会话id
                .stream() //流式输出
                //.call() //阻塞式输出
                .content();
    }
}
